package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.entity.merchant.DemandListEntity;
import com.sgb.link.entity.merchant.DemandType;
import com.sgb.link.utils.AddressHelper;
import com.sgb.merchant.api.MerchantsNet;

import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/7/28
 * @Description: 
 */
public class InvestmentDemandViewmodel extends BaseViewModel {
    public MutableLiveData<ListResponse<List<DemandListEntity>>> liveDataInvestment = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> liveDataCityList = new MutableLiveData<>();
    public MutableLiveData<List<DemandType>> ldType=new MutableLiveData<>();
    public MutableLiveData<Integer> ldState=new MutableLiveData<>();
    public int pageNum = 1;

    public InvestmentDemandViewmodel(@NonNull Application application) {
        super(application);
    }

    public void getInvestmentData(boolean isLoadMore, Map<String, Object> map) {
        pageNum = isLoadMore ? pageNum + 1 : 1;
        map.put("page", pageNum);
        map.put("limit", 20);
        MerchantsNet.getInstance().getService().getDemandList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<DemandListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<DemandListEntity>>> data) {
                if(data.isSuccess()){
                    liveDataInvestment.postValue(data.getData());
                }else{
                    liveDataInvestment.postValue(new ListResponse<List<DemandListEntity>>());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
                liveDataInvestment.postValue(new ListResponse<List<DemandListEntity>>());
            }
        });
    }

    /***
     * 获取需求类型
     */
    public void getType(){
        MerchantsNet.getInstance().getService().getType().enqueue(new ICallBack<BaseEntity<List<DemandType>>>() {
            @Override
            public void onSuccess(BaseEntity<List<DemandType>> data) {
                if(data.isSuccess()){
                    ldType.postValue(data.getData());
                }
            }
        });

    }
    /**
     * 获取服务城市
     */
    public void getServiceCity() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.CallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.size() > 0) {
                    liveDataCityList.postValue(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getState(String no){
        MerchantsNet.getInstance().getService().getDemandsState(no).enqueue(new ICallBack<BaseEntity<Integer>>() {
            @Override
            public void onSuccess(BaseEntity<Integer> data) {
                if(data.isSuccess()){
                    ldState.postValue(data.getData());
                }
            }
        });
    }
}
